package com.thu.tdnav;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.CursorAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;

public class ShowPlaceListActivity extends Activity{
	
	private DBHelper dbHelper=null;
	ArrayList<String> list=null;
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		tdnav appState = (tdnav) this.getApplication();
		appState.addActivity(this);
		Bundle extras=getIntent().getExtras();
		String attr=extras.getString("attr");
		setContentView(R.layout.showplace);
		try{
			dbHelper = new DBHelper(this);
			dbHelper.createDataBase();
			SQLiteDatabase db = dbHelper.getReadableDatabase();
			Cursor placeName = db.rawQuery("SELECT placename from place where attribute='"+attr+"'",null);
			ListView lv = (ListView)findViewById(R.id.list);
			list = new ArrayList<String>();
			while(placeName.moveToNext()){
				String placename=placeName.getString(placeName.getColumnIndex("placename"));
				list.add(placename);
			}
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(
				this,
				R.layout.listitem,
				list);
			lv.setAdapter(adapter);
			placeName.close();
			db.close();
			lv.setOnItemClickListener(new ItemClick());
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public final class ItemClick implements OnItemClickListener{
		
		public void onItemClick(AdapterView<?> parent,View view,int position,long id){
			Intent resultIntent = new Intent();
			Bundle bundle = new Bundle();
			bundle.putString("result",list.get(position));
			resultIntent.putExtras(bundle);
			ShowPlaceListActivity.this.setResult(RESULT_OK, resultIntent);
			ShowPlaceListActivity.this.finish();
		}
	}
	public void showDebugDialog(String title) {
		AlertDialog builder = new AlertDialog.Builder(this).setTitle(title)
				.setMessage("code uncompleted")
				.setPositiveButton("I know", null).show();
	}
}